package jschool.net.cliente;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

import jschool.net.Comando;

public class Cliente {

	private static Cliente cliente;
	
	private Socket sc = null;

	private InputStream input = null;
	private OutputStream output = null;
	private DataInputStream in = null;
	private DataOutputStream out = null;
	private ObjectInputStream ois = null;
	private ObjectOutputStream oos = null;
	
	
//--------------------------------------------------------------------------------
	
	private Cliente( String ip, int puerto ) {
		crearCliente( ip, puerto );
		crearComunicacion();
	}
	
//--------------------------------------------------------------------------------
	
	public static Cliente getCliente( String ip, int puerto ) {
		
		if( cliente == null ) {
			cliente = new Cliente( ip, puerto );
		}
		
		return cliente;
	}
	
//--------------------------------------------------------------------------------

	public void crearCliente(String ip, int puerto)
	{
	
		try
		{
		sc = new Socket (InetAddress.getByName( ip ), puerto);
		System.out.println("Conexion establecida");
		}
		
			catch (IOException ioe)
			{
			ioe.printStackTrace();
			System.out.println("\n\nERROR AL ESTABLECER LA CONEXION");
			}	
	}
	
//--------------------------------------------------------------------------------
	
	public void crearComunicacion()
	{
		try
		{
		output = sc.getOutputStream();
		out = new DataOutputStream( output );
		oos = new ObjectOutputStream( output );
		}
		
		catch(IOException ioe)
		{
		ioe.printStackTrace();
		System.out.println("Error al crear DataOutputStream");
		}	
		
//--------------------------		
		try
		{
		input = sc.getInputStream();
		in = new DataInputStream ( input );
		ois = new ObjectInputStream ( input );
		}
		
		catch(IOException ioe)
		{
		ioe.printStackTrace();
		System.out.println("Error al crear DataInputStream");
		}			
	}

//--------------------------------------------------------------------------------
	
	public void cerrarComunicacion()
	{
		if( sc != null && input != null && in != null && ois != null &&
			              output != null && out != null ) {
			try {
				in.close();
				input.close();			

				out.close();
				output.close();

				System.out.println("Comunicacion Cerrada");
			}
			catch (IOException ioe) {
				ioe.printStackTrace();
				System.out.println("\n\nERROR AL CERRAR LA CONEXION" );
			}
		}
	}
	
//--------------------------------------------------------------------------------
	
	public void comunicar( Comando comando, Object ... mensaje ) {
		
		try {
		oos.writeObject( comando );
			
			for( Object objeto : mensaje ) {
				oos.writeObject( objeto );
			}
		
		}
		catch( IOException ioe ) {
			ioe.printStackTrace();
		}
	}
	
//--------------------------------------------------------------------------------
}
